package com.zt.business.filter;

import com.zt.model.util.ConstUtil;
import lombok.extern.log4j.Log4j2;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@Log4j2
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //不拦截的去掉
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String requestURI = request.getRequestURI();
        if(requestURI.startsWith("/sys/login/")){
            filterChain.doFilter(request,response);
            return;
        }
        if(requestURI.endsWith("login.jsp")){
            filterChain.doFilter(request,response);
            return;
        }
        Object obj = request.getSession().getAttribute(ConstUtil.CURRENT_USER);
        if(Objects.nonNull(obj)){
            filterChain.doFilter(request,response);
            return;
        }
        log.debug("恭喜你，被拦截器了："+requestURI);
        response.sendRedirect("/login.jsp");
    }

    @Override
    public void destroy() {}
}
